package com.couldsail.takeaway.controller.user;

import com.couldsail.takeaway.common.result.Result;
import com.couldsail.takeaway.pojo.dto.ShoppingCartDTO;
import com.couldsail.takeaway.pojo.vo.ShoppingCartVO;
import com.couldsail.takeaway.service.ShoppingCartService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "购物车管理", description = "购物车相关接口")
@RestController("ShoppingCartController")
@RequestMapping("/user/shopping")
@RequiredArgsConstructor
@CrossOrigin(origins = "*") // 允许所有来源的请求跨域
public class ShoppingCartController {
    private final ShoppingCartService shoppingCartService;
    @GetMapping
    public Result<List<ShoppingCartVO>> getShoppingCart(Integer shopId){
        log.info("获取用户购物车{}",shopId);
        return Result.success(shoppingCartService.getShoppingCartList(shopId));
    }
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<String> updateShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("修改购物车数据{}",shoppingCartDTO);
        shoppingCartService.updateShoppingCart(shoppingCartDTO);
        return Result.success("修改成功");
    }
    @DeleteMapping("/del/{shopId}")
    public Result<String> deleteAllShoppingCart(@PathVariable("shopId") Integer shopId){
        log.info("用户清空购物列车{}",shopId);
        shoppingCartService.deleteAllShoppingCart(shopId);
        return Result.success("快去看看其他宝贝吧");
    }
}
